const router = require('koa-router')()
const CreateSequelize = require('./sequelize')
let sequelize = new CreateSequelize()
sequelize.init_sequeleze()

//获取点单页数据
router.get('/wx_data', async (ctx, next) => {
    let res = await sequelize.select('select * from wx_data')
    ctx.body = res
})
//加入购物车
router.get('/wx_addShop', async (ctx, next) => {
    let { data } = ctx.query
    let { img, name, price, state, hint, introduc, introduce, checked, num } = JSON.parse(data)
    let getData = await sequelize.select('select * from wx_shopdata')
    let index = getData.findIndex(item => item.name === name)
    if (index !== -1) {
        await sequelize.select(`update wx_shopdata set num="${getData[index].num + num}" where name='${getData[index].name}'`).catch(err => {
            console.log(err);
        })
    } else {
        await sequelize.select(`insert into wx_shopdata(img,name,price,state,hint,introduc,introduce,checked,num) values('${img}','${name}','${price}','${state}','${hint}','${introduc}','${introduce}','${checked}','${num}')`).catch(err => {
            console.log(err);
        })
    }
    let res = await sequelize.select('select * from wx_shopdata')
    ctx.body = res
})
//获取购物车数据
router.get('/wx_getShop', async (ctx, next) => {
    let res = await sequelize.select('select * from wx_shopdata')
    ctx.body = res
})
//修改复选框选中状态
router.get('/wx_Checked', async (ctx, next) => {
    let { data } = ctx.query
    let { checked, ifID } = JSON.parse(data)
    await sequelize.select(`update wx_shopdata set checked="${checked}" where ifID='${ifID}'`).catch(err => {
        console.log(err);
    })
    let res = await sequelize.select('select * from wx_shopdata')

    ctx.body = res
})
//全选
router.get('/wx_checkedAll', async (ctx, next) => {
    let { data } = ctx.query
    JSON.parse(data).forEach(async (item) => {
        await sequelize.select(`update wx_shopdata set checked="${item[0]}" where ifID='${item[1]}'`).catch(err => {
            console.log(err);
        })
    })
    let res = await sequelize.select('select * from wx_shopdata')
    ctx.body = res
})
//加
router.get('/wx_addCar', async (ctx, next) => {
    let { index } = ctx.query
    console.log(index);
    let shopData = await sequelize.select('select * from wx_shopdata')
    await sequelize.select(`update wx_shopdata set num="${shopData[index].num + 1}" where ifID='${shopData[index].ifID}'`).catch(err => {
        console.log(err);
    })
    let res = await sequelize.select('select * from wx_shopdata')
    ctx.body = res
})
//减
router.get('/wx_delCar', async (ctx, next) => {
    let { index } = ctx.query
    console.log(index);
    let shopData = await sequelize.select('select * from wx_shopdata')
    if (shopData[index].num > 1) {
        await sequelize.select(`update wx_shopdata set num="${shopData[index].num - 1}" where ifID='${shopData[index].ifID}'`).catch(err => {
            console.log(err);
        })
    } else {
        await sequelize.select(`delete from wx_shopdata where ifID='${shopData[index].ifID}'`).catch(err => {
            console.log(err);
        })
    }
    let res = await sequelize.select('select * from wx_shopdata')
    ctx.body = res
})

//----------------------------------------------------------------------

// 搜索页面中的数据
// 获取历史搜索记录信息
router.get('/getsearchhistory', async (ctx, next) => {
    let res = await sequelize.select('select * from searchhistory')
    ctx.body = res
})
// 添加历史记录
// router.post('/addHistory', async (ctx, next) => {
//     console.log(ctx.request,'----');


//     ctx.body = "添加成功"
// })
router.get('/addHistory', async (ctx, next) => {
    console.log(ctx.query, '----');
    let { res } = ctx.query
    if (res !== 'null') {
        await sequelize.select('insert into searchhistory(shopName) values("' + res + '")').catch(err => {
            console.log(err);
        })
        let newHistory = await sequelize.select('select * from searchhistory')

        ctx.body = newHistory
    } else {
        let newHistory = await sequelize.select('select * from searchhistory')

        ctx.body = newHistory
    }



})

// 清除历史记录
router.get('/delHistoryAll', async (ctx, next) => {
    await sequelize.select('delete from searchhistory').catch(err => {
        console.log(err);
    })

    let res = await sequelize.select('select * from searchhistory')
    ctx.body = res
})



router.get('/listAll', async (ctx, next) => {
    let res = await sequelize.select('select * from wx_order')
    ctx.body = res
})


router.get('/wx_getdata', async (ctx, next) => {
    // console.log(ctx.query.num);
    let data = await sequelize.select(`select * from miniprograme limit ${ctx.query.num * 6},6`);
    // console.log(data);
    ctx.body = data
})
router.get('/getinfo', async (ctx, next) => {
    console.log(ctx.query);
    let fgoods = await sequelize.select(`select * from miniprograme where id=${ctx.query.num}`)
    ctx.body = fgoods[0]
})
module.exports = router

